<script lang="ts">
export default {
  name: "Achievements"
};
</script>

<script setup lang="ts">
import { ECharts } from "echarts";
import echarts from "/@/plugins/echarts";
import { onMounted, nextTick, ref, PropType, watch } from "vue";
import { tryOnUnmounted, useTimeoutFn } from "@vueuse/core";
// import { useAppStoreHook } from "/@/store/modules/app";
import {
  addResizeListener,
  removeResizeListener
} from "element-plus/lib/utils/resize-event";

import { DataProps } from "./types";

const props = defineProps({
  echartData: {
    type: Array as PropType<DataProps[]>,
    default: () => []
  }
});
// const pureApp = useAppStoreHook();

const Achievements = ref<HTMLElement | null>(null) as any;
const chart = ref<HTMLElement | null>(null) as any;

let echartInstance: ECharts;

function initechartInstance(data) {
  const echartDom = chart.value;
  if (!echartDom) return;
  // @ts-ignore
  echartInstance = echarts.init(echartDom);
  echartInstance.clear(); //清除旧画布 重新渲染

  echartInstance.setOption({
    color: ["#00e6ad", "#59a0ff", "#ffc500"],
    tooltip: {
      trigger: "item"
    },
    // legend: {
    //   orient: "vertical",
    //   right: true
    // },
    // legend: {
    //   top: "5%",
    //   left: "center"
    // },
    series: [
      {
        name: "",
        type: "pie",
        label: {
          formatter: "{name|{b}} {time|{c} {d}%} ",

          rich: {
            time: {
              fontSize: 12
            }
          }
        },
        radius: ["30%", "60%"],
        data: data,
        //  [
        //   { value: 1079, name: "watchers" },
        //   { value: 1079, name: "star" },
        //   { value: 204, name: "forks" },
        //   { value: 3, name: "open_issues" }
        // ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)"
          }
        }
      }
    ]
  });
}

// onBeforeMount(() => {
//   nextTick(() => {
//     const data = props.echartData.map(item => {
//       item.value = item.stageTotal;
//       item.name = projectStage.get(item.stageStatus);
//     });
//     initechartInstance(data);
//   });
// });

watch(
  () => props.echartData,
  newVal => {
    if (newVal.length) {
      nextTick(() => {
        let arr = [];
        arr = arr.concat(newVal);
        initechartInstance(arr);
      });
    }
  }
);

onMounted(() => {
  nextTick(() => {
    // initechartInstance(props.echartData);
    addResizeListener(Achievements.value, resetRender);
    // useEventListener("resize", () => {
    //   resetRender();
    // });
  });
});

// watch(
//   () => pureApp.sidebar.opened,
//   () => {
//     if(pureApp.sidebar.opened){
//       echartInstance.dispose();
//        echartInstance = null;
//        useTimeoutFn(() => {
//          nextTick(() => {
//            initechartInstance();
//          })
//        }, 300);
//     }
//   }
// );

const resetRender = () => {
  if (!echartInstance) return;
  useTimeoutFn(() => {
    echartInstance.resize();
  }, 180);
};

tryOnUnmounted(() => {
  if (!echartInstance) return;
  echartInstance.dispose();
  echartInstance = null;
  removeResizeListener(Achievements.value, resetRender);
});
</script>

<template>
  <div ref="Achievements">
    <div class="title">
      <slot> </slot>
    </div>
    <div ref="chart" class="Achievements"></div>
  </div>
</template>

<style lang="scss" scoped>
.title {
  font-size: 16px;
  color: $defaultColor;
  margin: 10px 0;
  height: 24px;
}
.Achievements {
  width: 100%;
  height: 300px;
}
</style>
